define(['jquery','jquerymobile','utils','require'],function($,jqm,utils,require) {
    console.log("backend loaded");
    
    function sendRequest(type, url, data, successCallback, failureCallback) {
        $.ajax({
            url: url,
            type: type,
            data: data,
            beforeSend: function(xhr) {
                xhr.setRequestHeader(utils.appConfig().accessTokenName(), 
                                     utils.appConfig().accessTokenValue());
            },
        })
        .done(successCallback)
        .fail(failureCallback);
    }

    function getBaseUrl() {
        var host = utils.appConfig().serverHost();
        var base_url = host + "/" + utils.appConfig().projectName;

        return base_url;
    }

    function send(method, url, data, callback, failCallHandler) {
        sendRequest(
                        method, 
                        getBaseUrl() + url, 
                        data, 
                        function(rdata, status) {
                           if (isAppTimeout(rdata, status)) {
                                // Relogin
                                require("presenter").userLogin(
                                    utils.appConfig().userName(),
                                    utils.appConfig().userPassword(),
                                    function(result) {
                                        var resultCode = result.resultCode;
                                        var accessToken = result.token;

                                        if (1 === resultCode) {
                                            utils.appConfig().accessTokenValue(accessToken);
                                            // Resend the request once login
                                            send(method, url, data, callback, failCallHandler);
                                        }
                                    }
                                  );
                           }
                           else 
                               callback(rdata, status);
                        }, 
                        failCallHandler
                   );
    }

    function get(url, data, callback, failCallHandler) {
        send('GET', url, data, callback, failCallHandler);
    }

    function post(url, data, callback, failCallHandler) {
        send('POST', url, data, callback, failCallHandler);
    }

    function isAppTimeout(data, status) {
        var result = false;

        if ('success' === status) {
            var rawList = JSON.parse(data);
            
            // Check if timeout
            if (!utils.isObjEmpty(rawList) &&
                !utils.isObjEmpty(rawList[0]) &&
                !utils.isObjEmpty(rawList[0].resultCode) &&
                '998' === rawList[0].resultCode &&
                !utils.isStrEmpty(utils.appConfig().accessTokenValue())) {
                
                result = true;
                console.log("The app is timeout!");
            }

        }

        return result;
    }

    return {
        get: get,
        post: post,
    }
});
